<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

/**
 * Class SaleHangingBill
 * @package app\home\controller
 * 销售挂帐单
 */
class SaleHangingBill extends BaseController
{
    protected $table='sale_hanging_bill';
    protected $title='销售挂帐单';
    private $sessionKey='pageSaleHangingBill';

    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DbException
     * date:2024/5/8
     */
    public function index()
    {
        if (request()->isAjax()) {
            $where[]=['pp.status','>',-1];
            $where[]=['pp.h_type','=',2];
            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['pp.status','=',$this->param['status']];
            }
            if (isset($this->param['type'])&&$this->param['type']){
                $where[]=['pp.type','=',$this->param['type']];
            }
            if (isset($this->param['g_orderNum'])&&$this->param['g_orderNum']){
                $where[]=['pp.g_orderNum','like',"%".$this->param['g_orderNum'].'%'];
            }
            foreach (['p_code','p_name','md_bar_code','md_name','md_brand','md_specs','username'] as $v){
                if ($v=='md_bar_code'){
                    $k='md.bar_code';
                }else{
                    $k=str_replace('_','.',$v);
                }
                if (isset($this->param[$v])&&$this->param[$v]){
                    $where[]=[$k,'like','%'.$this->param[$v].'%'];
                }
            }
            if (isset($this->param['pp_time'])&&$this->param['pp_time']){
                $pptim=explode('~',$this->param['pp_time']);
                $bt=$pptim[0];
                $et=$pptim[1];
                $where[]=['pp.orderDate','between',[$bt,$et]];
            }
            session($this->sessionKey,$where);
            $rows = empty($this->param['limit']) ? get_config('app.page_size') : $this->param['limit'];
            $list = Db::name($this->table)->alias('pp')
                ->join('material_dossier md','md.id=pp.material_dossier_id')
                ->join('purveyor p','p.id=pp.punter_id','left')
                ->join('admin a','a.id=pp.create_user','left')
                ->where($where)
                ->order('pp.id DESC')
                ->field('pp.*,md.bar_code,md.name as mdName,md.unit,md.specs,p.name as pName,p.code as pCode')
                ->paginate($rows)->each(function ($item,$index){
                    $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
                        ->where('sale_hanging_bill_id',$item['id'])->sum('reconcileNum');
//                    if ($item['punter_begin_id']){
//                        $item['num']=Db::name('punter_begin')->where('id',$item['punter_begin_id'])->value('num');
//                    }elseif($item['out_receipt_detail_id']){
//                        $item['num']=Db::name('out_receipt_detail')->where('id',$item['out_receipt_detail_id'])->value('num');
//                    }elseif ($item['in_receipt_detail_id']){
//                        $item['num']=Db::name('in_receipt_detail')->where('id',$item['in_receipt_detail_id'])->value('num');
//                    }else{
//                        $item['num']=0;
//                    }
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

    public function exportData()
    {
        $where=session($this->sessionKey);
        $mStatus=['','未对账','部分对账','已对账'];
        $list = Db::name($this->table)->alias('pp')
            ->join('material_dossier md','md.id=pp.material_dossier_id')
            ->join('purveyor p','p.id=pp.punter_id','left')
            ->join('admin a','a.id=pp.create_user','left')
            ->where($where)
            ->order('pp.id DESC')
            ->field('pp.*,md.bar_code,md.name as mdName,md.unit,md.specs,p.name as pName,p.code as pCode')
            ->select()->each(function ($item,$index) use ($mStatus){
                $item['dzNum']=Db::name('sale_reconcile')->where('status',2)
                    ->where('sale_hanging_bill_id',$item['id'])->sum('reconcileNum');
                $item['status']=$mStatus[$item['status']];
                return $item;
            });
        $title = [
//            'id' => 'ID',
//            'orderNum' => '单据编码',
            'pName' => '客户名称',
            'bar_code' => '产品编码',
            'mdName' => '产品名称',
            'specs' => '规格型号',
            'unit' => '单位',
            'num' => '挂账数量',
            'dzNum' => '对账数量',
            'type' => '业务类型',
            'g_orderNum' => '关联单据编号',
            'orderDate' => '日期',
            'status' => '状态',

        ];
        $name =$this->title;
        exportXlsData($name,$title,$list);
        exit();
    }
}